package demo6;

import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        /*if (this.age - o.age > 0){
            return 1;
        }else if (this.age - o.age == 0){
            return 0;
        }else{
            return -1;
        }*/
        return this.age-o.age;//从小到大排序
//        return o.age-this.age;//从大到小排序
    }
}




public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",12,89.2);
        students[1] = new Student("lisi",15,69.2);
        students[2] = new Student("wangwu",14,81.9);
        bubbleSort(students);
        System.out.println("排序后的数组：" + Arrays.toString(students));
    }
    public static void bubbleSort(Comparable[] comparables){
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if (comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    public static void main3(String[] args) {
        Student student1 = new Student("zhangsan",12,89.2);
        Student student2 = new Student("lisi",15,69.2);
        int ret = student1.compareTo(student2);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",12,30.4);
        students[1] = new Student("lisi",15,70.3);
        students[2] = new Student("wangwu",14,81.9);
        System.out.println("排序前的students:" + Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后的students:" + Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {1,5,2,5,6};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
